import { LOGIN_PAGE, LOGIN, COURSELIST, ACCOUNTMANAGE, PANTOKEN, WEBIM } from "../../../configs/api";
import { request } from '../../../utils/request'
import store from '../../../utils/file'
import AES from "../../../utils/AES";
const userLogin = async (uname: string, password: string, encrypt=true): Promise<UserCookieType | 'AuthFailed'> => {
  let params: UserCookieType = {
    fid: '-1', pid: '-1', refer: 'http%3A%2F%2Fi.chaoxing.com', _blank: '1', t: true,
    vc3: null, _uid: null, _d: null, uf: null
  }
  // 密码DES加密
  if (encrypt){
    password = AES(password)
  } 
  // 填充表单
  let formdata = `uname=${uname}&password=${password}&fid=-1&t=true&refer=https%253A%252F%252Fi.chaoxing.com&forbidotherlogin=0&validate=`
  // 这行代码看起来没啥用,为啥超星工具要加
  // let res = await request({
  //   url: LOGIN_PAGE.URL,
  //   method: LOGIN_PAGE.METHOD as any,
  // })
  let data = await request({
    url: LOGIN.URL,
    method: LOGIN.METHOD as any,
    header: {
      'Content-Type': 'application/x-www-form-urlencoded',
      'X-Requested-With': 'XMLHttpRequest'
    },
    data: formdata,
  })
  // 登录失败
  console.log(data)
  if (!(data.data as any).status) return (data.data as any).msg2;
  let c_equal, c_semi, itemName, itemValue, rt_cookies
  const map = new Map()
  const cookies = data.cookies
  for (let i = 0; i < cookies!.length; i++) {
    c_equal = cookies![i].indexOf('=')
    c_semi = cookies![i].indexOf(';')
    itemName = cookies![i].substring(0, c_equal)
    itemValue = cookies![i].substring(c_equal + 1, c_semi)
    map.set(itemName, itemValue)
  }
  rt_cookies = Object.fromEntries(map.entries())
  params = Object.assign(params, rt_cookies)
  return params;
}
/**
 * 
 * @param uname 这个实际上是手机号
 * @param params 
 */
const storeUser = (uname: string, params: UserCookieType) => {
  store.set(uname, params)
}
const loginOut = (uname:string) => {
  store.remove(uname)
}

const storeUserName = (userRealName: string) => {
  store.set('userRealName', userRealName)
}

const getUserRealName = () => {
  return store.get('userRealName') as string;
}

const getUser = (uname: string) => {
  // 当用户cookie不存在让他重新登录
  const params = store.get(uname) as UserCookieType
  if (params) {
    wx.navigateTo({
      url: '../../../../miniprogram/pages/login/login'
    })
  }
  return params
}

const getAccountInfo = async (uf: string, _d: string, _uid: string, vc3: string): Promise<string> => {
  let res = await request({
    url: ACCOUNTMANAGE.URL,
    method: ACCOUNTMANAGE.METHOD as any,
    header: {
      'Cookie': `uf=${uf}; _d=${_d}; UID=${_uid}; vc3=${vc3};`
    }
  })
  const data = res.data as string
  let end_of_messageName = data.indexOf('<li><p class="fr colorBlue">') + 28
  let name = data.slice(end_of_messageName, data.indexOf('<', end_of_messageName))
  storeUserName(name);
  return name;
}


// 返回当前用户的用户名
const getCurUser = (): string => {
  return getApp().globalData.curUser;
}





export default {
  userLogin,
  storeUser,
  getUser,
  getAccountInfo,
  getCurUser,
  getUserRealName,
  loginOut,
  storeUserName
}
